home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gwuada_6.zip / WRITE.C < prev   
C/C++ Source or Header  |  1993-04-11  |  3KB  |  119 lines

  1. /*
  2.     GWAda Development Environment for 386/486 PCs   
  3.     Copyright (C) 1993, Arthur Vargas Lopes  & Michael Bliss Feldman
  4.                         vlopes@vortex.ufrgs.br mfeldman@seas.gwu.edu
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; version 2 of the License.    
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. /* write.c */
  21.  
  22. #include "externs.h"
  23.  
  24.  
  25.  
  26. void AVL_WRITE_OUT()
  27. {
  28.     AVL_EDIT_WINDOW_PTR w;
  29.     AVL_WIN_PTR m1, m2;
  30.     int n1, n2, x, from_here = 0, till_here = 0;
  31.     char fname[80];
  32.     static char *msg = "GWAda - Put to which file? ";
  33.     char msg2[80];
  34.     AVL_LINE_PTR temp, temp2 = NULL;
  35.     int fp, i;
  36.     int line_no = 0;
  37.     long fsize;
  38.     char *buf;
  39.     char line[1024];
  40.     unsigned int size, n, j;
  41.     if (avl_block_first_line == NULL ||
  42.         avl_block_last_line == NULL) {
  43.         AVL_ERROR("Mark a block first.");
  44.         return;
  45.         }
  46.     w = &avl_windows[avl_window];
  47.     n1 = 62;
  48.     n2 = (80 - n1) / 2;
  49.     m1 = AVL_MAKE_WINDOW(msg,7,n2,9,n1+n2,avl_wnd_bk_color,avl_wnd_color);
  50.     sprintf(fname,"%s%cblock",avl_dir_sources,'\\');
  51.     AVL_PROMPT(1,1,fname,60);
  52.     sprintf(msg2,"Writing \'%s\'", fname);
  53.     n1 = strlen(msg2);
  54.     if (n1 < 17) n1 = 17;
  55.     n1 += 5;
  56.     n2 = (80 - n1) / 2;
  57.     m2 = AVL_MAKE_WINDOW("",23,1,25,strlen(msg2)+3,avl_wnd_bk_color,avl_wnd_color);
  58.     _setbkcolor(avl_msg_bk_color);
  59.     _settextcolor(avl_msg_color);
  60.     _settextposition(1,1);
  61.     _outtext(msg2);
  62.     unlink(fname);       
  63.     fp = open(fname,O_CREAT | O_RDWR,S_IREAD | S_IWRITE);
  64.     if (fp == -1)  {
  65.         sprintf(msg2,"Could not open \'%s\'\n", fname);
  66.         AVL_ERROR(msg2);
  67.         AVL_DEL_WINDOW(m2);
  68.         AVL_DEL_WINDOW(m1);
  69.         return;
  70.         }
  71.     size = 100000L;
  72.     if ((buf = malloc(size)) == NULL)  { 
  73.         AVL_ERROR("Out of memory!");
  74.         AVL_DEL_WINDOW(m2);
  75.         AVL_DEL_WINDOW(m1);
  76.         return;
  77.         }
  78.     /* FIll in buffer */
  79.     j = 0;
  80.     temp = avl_block_first_line;
  81.     from_here = avl_block_first_col;
  82.     while ( 1 )  {
  83.         if (j >= size)  {  /*  flush buffer to disk  */
  84.             n = write(fp,buf,size);
  85.             if (n != size) {
  86.                 sprintf(msg2,"Could not write \'%s\'\n", fname);
  87.                 AVL_ERROR(msg2);
  88.                 AVL_DEL_WINDOW(m2);
  89.                 AVL_DEL_WINDOW(m1);
  90.                 return;
  91.                 }
  92.             fsize -= n;
  93.             j = 0;
  94.             }
  95.         if (temp == avl_block_last_line)
  96.             till_here = avl_block_last_col + 1;
  97.         else
  98.             till_here = strlen(temp -> line);
  99.         for(i = 0 + from_here; i < till_here; ++i)
  100.             buf[j++] = temp -> line[i];
  101.         if (temp -> next != avl_block_first_line)
  102.             buf[j++] = '\n';
  103.         else
  104.             break;
  105.         temp = temp -> next;
  106.         } 
  107.     n = write(fp,buf,j);
  108.     if (n != j) {
  109.         sprintf(msg2,"Could not write \'%s\'\n", fname);
  110.         AVL_ERROR(msg2);
  111.         }
  112.     else {
  113.         close(fp);
  114.         free(buf);
  115.         }
  116.     AVL_DEL_WINDOW(m2);
  117.     AVL_DEL_WINDOW(m1);
  118. }
  119.